package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.constant.CacheConstant;
import cc.admin.common.constant.CommonConstant;
import cc.admin.common.sys.api.ISysBaseAPI;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.util.JwtUtil;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.common.util.PasswordUtil;
import cc.admin.common.util.PmsUtil;
import cc.admin.common.util.RedisUtil;
import cc.admin.common.util.oConvertUtils;
import cc.admin.modules.shiro.authc.ShiroRealm;
import cc.admin.modules.sys.entity.*;
import cc.admin.modules.sys.mapper.SysUserMapper;
import cc.admin.modules.sys.model.DepartIdModel;
import cc.admin.modules.sys.model.SysDepartTreeModel;
import cc.admin.modules.sys.model.SysUserSysDepartModel;
import cc.admin.modules.sys.service.*;
import cc.admin.modules.sys.vo.SysDepartUsersVO;
import cc.admin.modules.sys.vo.SysUserRoleVO;
import cc.admin.poi.excel.ExcelImportUtil;
import cc.admin.poi.excel.def.NormalExcelConstants;
import cc.admin.poi.excel.entity.ExportParams;
import cc.admin.poi.excel.entity.ImportParams;
import cc.admin.poi.excel.view.PoiEntityExcelView;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.log.Log;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.Assert;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <getPageFilterFields>
 * 用户表 前端控制器
 * </getPageFilterFields>
 *
 * @Author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private ISysDepartService sysDepartService;

	@Autowired
	private ISysUserRoleService sysUserRoleService;

	@Autowired
	private ISysUserDepartService sysUserDepartService;

	@Autowired
	private ISysDepartRoleUserService departRoleUserService;

	@Autowired
	private ISysDepartRoleService departRoleService;

	@Autowired
	private ISysRoleService sysRoleService;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	SysUserMapper sysUserMapper;

	@Value("${cc.admin.path.upload}")
	private String upLoadPath;

	/**
	 * 获取用户列表数据
	 *
	 * @param user
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysUser>> queryPageList(SysUser user, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
												@RequestParam(name = "sysDeptCode", required = false) String sysDeptCode, HttpServletRequest req) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Result<IPage<SysUser>> result = new Result<IPage<SysUser>>();
//		QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(sysDeptCode)) {
			queryWrapper.likeRight("sys_dept_code", sysDeptCode);
		}
		if (StrUtil.isNotEmpty(user.getIsAdmin())) {
			queryWrapper.eq("is_admin", user.getIsAdmin());
		}
		// 非管理员不能看到管理员
		if ("0".equals(loginUser.getIsAdmin())) {
			queryWrapper.ne("is_admin", "1");
		}
		if (user.getPost() != null && user.getPost() != "") {
			queryWrapper.eq("post", user.getPost());
		}
		if (user.getPhone() != null && user.getPhone() != "") {
			queryWrapper.like("phone", user.getPhone());
		}
		if (user.getUsername() != null && user.getUsername() != "") {
			queryWrapper.like("username", user.getUsername());
		}
		if (user.getRealname() != null && user.getRealname() != "") {
			queryWrapper.like("realname", user.getRealname());
		}
		if (StrUtil.isNotEmpty(user.getType())) {
			queryWrapper.eq("type", user.getType());
		}
		if (!"1".equals(loginUser.getId())) {
			queryWrapper.ne("id", "1");
		}

		//queryWrapper.ne("id", loginUser.getId());
		queryWrapper.ne("username", "_reserve_user_external");
		Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
		IPage<SysUser> pageList = sysUserService.page(page, queryWrapper);
		//把用户角色信息补充进去
		pageList.getRecords().forEach(sysUser -> {
			QueryWrapper<SysRole> roleWrapper = new QueryWrapper<>();
			roleWrapper.nested(i -> i.inSql("id", String.format("select role_id from sys_user_role where user_id='%s'", sysUser.getId())));
			List<SysRole> roleList = sysRoleService.list(roleWrapper);
			List<String> roleIdList = roleList.stream().map(SysRole::getId).collect(Collectors.toList());
			List<String> roleNames = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
			sysUser.setUserRoleNames(CollectionUtil.join(roleNames, ","));
			sysUser.setUserRoleIdList(roleIdList);
		});
		result.setSuccess(true);
		result.setResult(pageList);
		log.info(pageList.toString());
		return result;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<SysUser> add(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		String selectedRoles = jsonObject.getString("selectedroles");
		String selectedDeparts = jsonObject.getString("selecteddeparts");
		String userName = jsonObject.getString("username").trim();
		Assert.isTrue(!sysUserService.checkUserExist(userName),"用户已经存在!");
		try {
			SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
			user.setUsername(userName);
			user.setCreateTime(new Date());//设置创建时间
			String salt = oConvertUtils.randomGen(8);
			user.setSalt(salt);
			String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt);
			user.setPassword(passwordEncode);
			user.setStatus(1);
			user.setUserFlag("0");
			user.setIsAdmin("0");
			user.setFirstLogin("0");
			user.setDelFlag(CommonConstant.DEL_FLAG_0);
			Date endTime = user.getEndTime();
			if (endTime != null) {
				endTime.setHours(23);
				endTime.setMinutes(59);
				endTime.setSeconds(59);
				user.setEndTime(endTime);
			}
			sysUserService.addUserWithRole(user, selectedRoles);
			sysUserService.addUserWithDepart(user, selectedDeparts);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}

	@RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
	public Result<SysUser> addAdmin(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		String selectedRoles = jsonObject.getString("selectedroles");
		String userName = jsonObject.getString("username");
		Assert.isTrue(!sysUserService.checkUserExist(userName),"用户已经存在!");
		try {
			SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
			user.setCreateTime(new Date());//设置创建时间
			String salt = oConvertUtils.randomGen(8);
			user.setSalt(salt);
			String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt);
			user.setPassword(passwordEncode);
			user.setStatus(1);
			user.setUserFlag("0");
			user.setDelFlag(CommonConstant.DEL_FLAG_0);
			user.setIsAdmin("0");
			user.setType("3");
			sysUserService.addUserWithRole(user, selectedRoles);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}

	@RequestMapping(value = "/setUserRoles", method = RequestMethod.PUT)
	public Result<?> setUserRoles(@RequestParam(name = "userId", required = true) String userId,
								  @RequestParam(name = "selectedRoles", required = true) String selectedRoles) {
		//添加成功之后 清除缓存
		DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
		ShiroRealm shiroRealm = (ShiroRealm) securityManager.getRealms().iterator().next();
		//清除权限 相关的缓存
		shiroRealm.clearAllCache();
		SysUser sysUser = sysUserService.getById(userId);
		if (sysUser == null) {
			return Result.error("未找到对应实体");
		} else {
			sysUserService.editUserWithRole(sysUser, selectedRoles);
		}
		return Result.ok("保存成功");
	}

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public Result<SysUser> edit(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			SysUser sysUser = sysUserService.getById(jsonObject.getString("id"));
			sysBaseAPI.addLog("编辑用户，id： " + jsonObject.getString("id"), CommonConstant.LOG_TYPE_2, 2);
			if (sysUser == null) {
				result.error500("未找到对应实体");
			} else {
				SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
				user.setUpdateTime(new Date());
				//String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), sysUser.getSalt());
				user.setPassword(sysUser.getPassword());
				Date endTime = user.getEndTime();
				if (endTime != null) {
					endTime.setHours(23);
					endTime.setMinutes(59);
					endTime.setSeconds(59);
					user.setEndTime(endTime);
				}
				sysUserService.updateById(user);
				sysUserService.updateNullPhoneEmail();
				result.success("修改成功!");
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 * 删除用户
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		sysBaseAPI.addLog("删除用户，id： " + id, CommonConstant.LOG_TYPE_2, 3);
		this.sysUserService.deleteUser(id);
		return Result.ok("删除用户成功");
	}

	/**
	 * 批量删除用户
	 */
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		sysBaseAPI.addLog("批量删除用户， ids： " + ids, CommonConstant.LOG_TYPE_2, 3);
		this.sysUserService.deleteBatchUsers(ids);
		return Result.ok("批量删除用户成功");
	}

	/**
	 * 冻结&解冻用户
	 *
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping(value = "/frozenBatch", method = RequestMethod.PUT)
	public Result<SysUser> frozenBatch(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			String ids = jsonObject.getString("ids");
			String status = jsonObject.getString("status");
			String[] arr = ids.split(",");
			for (String id : arr) {
				if (oConvertUtils.isNotEmpty(id)) {
					this.sysUserService.update(new SysUser().setStatus(Integer.parseInt(status)),
							new UpdateWrapper<SysUser>().lambda().eq(SysUser::getId, id));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败" + e.getMessage());
		}
		result.success("操作成功!");
		return result;

	}

	@RequestMapping(value = "/queryById", method = RequestMethod.GET)
	public Result<SysUser> queryById(@RequestParam(name = "id", required = true) String id) {
		Result<SysUser> result = new Result<SysUser>();
		SysUser sysUser = sysUserService.getById(id);
		if (sysUser == null) {
			result.error500("未找到对应实体");
		} else {
			result.setResult(sysUser);
			result.setSuccess(true);
		}
		return result;
	}

	@RequestMapping(value = "/queryUserRole", method = RequestMethod.GET)
	public Result<List<String>> queryUserRole(@RequestParam(name = "userid", required = true) String userid) {
		Result<List<String>> result = new Result<>();
		List<String> list = new ArrayList<String>();
		List<SysUserRole> userRole = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userid));
		if (userRole == null || userRole.size() <= 0) {
			result.error500("未找到用户相关角色信息");
		} else {
			for (SysUserRole sysUserRole : userRole) {
				list.add(sysUserRole.getRoleId());
			}
			result.setSuccess(true);
			result.setResult(list);
		}
		return result;
	}

	/**
	 * 用户登录保密协议授权
	 */
	@PostMapping("/consent")
	public Result<?> consent(@RequestParam String username) {
		sysUserService.consent(username);
		return Result.ok();
	}

	/**
	 * 校验用户账号是否存在正常状态的
	 *
	 * @param userName
	 * @return true存在  false 不存在
	 */
	@RequestMapping(value = "/checkUserExist", method = RequestMethod.GET)
	public Result<Object> checkUserExist(@RequestParam(name = "userName", required = true) String userName) {
		return Result.ok(sysUserService.checkUserExist(userName));
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "/changePassword", method = RequestMethod.PUT)
	public Result<?> changePassword(@RequestBody SysUser sysUser) {
		SysUser u = this.sysUserService.getUserByName(sysUser.getUsername());
		if (u == null) {
			return Result.error("用户不存在！");
		}
		// 修改密码需要确认原来的密码是否正确
		String userpassword = PasswordUtil.encrypt(sysUser.getUsername(), sysUser.getOldPassword(), u.getSalt());
		String syspassword = u.getPassword();
		if (!syspassword.equals(userpassword)) {
			return Result.error("原始密码错误！");
		}
		sysUser.setId(u.getId());
		sysUser.setFirstLogin("0");
		sysUser.setUpdateTime(new Date());
		return sysUserService.changePassword(sysUser);
	}


	/**
	 * 重置密码
	 */
	@RequestMapping(value = "/resetPassword", method = RequestMethod.PUT)
	public Result<?> resetPassword(@RequestBody SysUser sysUser) {
		SysUser u = this.sysUserService.getUserByName(sysUser.getUsername());
		if (u == null) {
			return Result.error("用户不存在！");
		}
		sysUser.setId(u.getId());
		sysUser.setFirstLogin("1");
		sysUser.setUpdateTime(new Date());
		return sysUserService.changePassword(sysUser);
	}

	/**
	 * 用户自己修改密码
	 */
	@RequestMapping(value = "/changePasswordByUser", method = RequestMethod.PUT)
	public Result<?> changePasswordByUser(@RequestBody SysUser sysUser) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		sysUser.setId(user.getId());
		return sysUserService.changePassword(sysUser);
	}

	/**
	 * 用户自己修改密码
	 */
	@RequestMapping(value = "/passwordByUser", method = RequestMethod.PUT)
	public Result<?> passwordByUser(@RequestBody SysUser sysUser) {
		SysUser user = sysUserService.getUserByName(sysUser.getUsername());
		if (user.getId().equals(sysUser.getId())) {
			user.setPassword(sysUser.getPassword());
			return sysUserService.changePassword(user);
		}
		return Result.error("用户不存在！");

	}

	/**
	 * 查询指定用户和部门关联的数据
	 *
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/userDepartList", method = RequestMethod.GET)
	public Result<List<DepartIdModel>> getUserDepartsList(@RequestParam(name = "userId", required = true) String userId) {
		Result<List<DepartIdModel>> result = new Result<>();
		try {
			List<DepartIdModel> depIdModelList = this.sysUserDepartService.queryDepartIdsOfUser(userId);
			if (depIdModelList != null && depIdModelList.size() > 0) {
				result.setSuccess(true);
				result.setMessage("查找成功");
				result.setResult(depIdModelList);
			} else {
				result.setSuccess(false);
				result.setMessage("查找失败");
			}
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setSuccess(false);
			result.setMessage("查找过程中出现了异常: " + e.getMessage());
			return result;
		}

	}

	/**
	 * 生成在添加用户情况下没有主键的问题,返回给前端,根据该id绑定部门数据
	 *
	 * @return
	 */
	@RequestMapping(value = "/generateUserId", method = RequestMethod.GET)
	public Result<String> generateUserId() {
		Result<String> result = new Result<>();
		System.out.println("我执行了,生成用户ID==============================");
		String userId = UUID.randomUUID().toString().replace("-", "");
		result.setSuccess(true);
		result.setResult(userId);
		return result;
	}

	/**
	 * 根据部门id查询用户信息
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryUserByDepId", method = RequestMethod.GET)
	public Result<List<SysUser>> queryUserByDepId(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "realname", required = false) String realname) {
		Result<List<SysUser>> result = new Result<>();
		//List<SysUser> userList = sysUserDepartService.queryUserByDepId(id);
		SysDepart sysDepart = sysDepartService.getById(id);
		// List<SysUser> userList = sysUserDepartService.queryUserByDepCode(sysDepart.getSysOrgCode(),realname);
		//批量查询用户的所属部门
		//step.1 先拿到全部的 useids
		//step.2 通过 useids，一次性查询用户的所属部门名字
//        List<String> userIds = userList.stream().map(SysUser::getId).collect(Collectors.toList());
//        if(userIds!=null && userIds.size()>0){
//            Map<String,String>  useDepNames = sysUserService.getDepNamesByUserIds(userIds);
//            userList.forEach(item->{
//                item.setSysOrgCode(useDepNames.get(item.getId()));
//            });
//        }
		try {
			result.setSuccess(true);
			//  result.setResult(userList);
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setSuccess(false);
			return result;
		}
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(SysUser sysUser, HttpServletRequest request) {
		// Step.1 组装查询条件
		QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(sysUser, request.getParameterMap());
		//Step.2 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new PoiEntityExcelView());
		//update-begin--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据--------------------
		String selections = request.getParameter("selections");
		if (!oConvertUtils.isEmpty(selections)) {
			queryWrapper.in("id", selections.split(","));
		}
		//update-end--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据----------------------
		List<SysUser> pageList = sysUserService.list(queryWrapper);
		//导出文件名称
		mv.addObject(NormalExcelConstants.FILE_NAME, "用户列表");
		mv.addObject(NormalExcelConstants.CLASS, SysUser.class);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ExportParams exportParams = new ExportParams("用户列表数据", "导出人:" + user.getRealname(), "导出信息");
		exportParams.setImageBasePath(upLoadPath);
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		// 错误信息
		List<String> errorMessage = new ArrayList<>();
		int successLines = 0, errorLines = 0;
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
				for (int i = 0; i < listSysUsers.size(); i++) {
					SysUser sysUserExcel = listSysUsers.get(i);
					if (StringUtils.isBlank(sysUserExcel.getPassword())) {
						// 密码默认为 “123456”
						sysUserExcel.setPassword("123456");
					}
					// 密码加密加盐
					String salt = oConvertUtils.randomGen(8);
					sysUserExcel.setSalt(salt);
					String passwordEncode = PasswordUtil.encrypt(sysUserExcel.getUsername(), sysUserExcel.getPassword(), salt);
					sysUserExcel.setPassword(passwordEncode);
					try {
						sysUserService.save(sysUserExcel);
						successLines++;
					} catch (Exception e) {
						errorLines++;
						String message = e.getMessage();
						int lineNumber = i + 1;
						// 通过索引名判断出错信息
						if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_USERNAME)) {
							errorMessage.add("第 " + lineNumber + " 行：用户名已经存在，忽略导入。");
						} else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_WORK_NO)) {
							errorMessage.add("第 " + lineNumber + " 行：工号已经存在，忽略导入。");
						} else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_PHONE)) {
							errorMessage.add("第 " + lineNumber + " 行：手机号已经存在，忽略导入。");
						} else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_EMAIL)) {
							errorMessage.add("第 " + lineNumber + " 行：电子邮件已经存在，忽略导入。");
						} else {
							errorMessage.add("第 " + lineNumber + " 行：未知错误，忽略导入");
							log.error(e.getMessage(), e);
						}
					}
					// 批量将部门和用户信息建立关联关系
					String departIds = sysUserExcel.getDepartIds();
					if (StringUtils.isNotBlank(departIds)) {
						String userId = sysUserExcel.getId();
						String[] departIdArray = departIds.split(",");
						List<SysUserDepart> userDepartList = new ArrayList<>(departIdArray.length);
						for (String departId : departIdArray) {
							userDepartList.add(new SysUserDepart(userId, departId));
						}
						sysUserDepartService.saveBatch(userDepartList);
					}

				}
			} catch (Exception e) {
				errorMessage.add("发生异常：" + e.getMessage());
				log.error(e.getMessage(), e);
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
		if (errorLines == 0) {
			return Result.ok("共" + successLines + "行数据全部导入成功！");
		} else {
			JSONObject result = new JSONObject(5);
			int totalCount = successLines + errorLines;
			result.put("totalCount", totalCount);
			result.put("errorCount", errorLines);
			result.put("successCount", successLines);
			result.put("msg", "总上传行数：" + totalCount + "，已导入行数：" + successLines + "，错误行数：" + errorLines);
			String fileUrl = PmsUtil.saveErrorTxtByList(errorMessage, "userImportExcelErrorLog");
			int lastIndex = fileUrl.lastIndexOf(File.separator);
			String fileName = fileUrl.substring(lastIndex + 1);
			result.put("fileUrl", "/sys/common/static/" + fileUrl);
			result.put("fileName", fileName);
			Result res = Result.ok(result);
			res.setCode(201);
			res.setMessage("文件导入成功，但有错误。");
			return res;
		}
	}

	/**
	 * @param userIds
	 * @return
	 * @功能：根据id 批量查询
	 */
	@RequestMapping(value = "/queryByIds", method = RequestMethod.GET)
	public Result<Collection<SysUser>> queryByIds(@RequestParam String userIds) {
		Result<Collection<SysUser>> result = new Result<>();
		String[] userId = userIds.split(",");
		Collection<String> idList = Arrays.asList(userId);
		Collection<SysUser> userRole = sysUserService.listByIds(idList);
		result.setSuccess(true);
		result.setResult(userRole);
		return result;
	}

	/**
	 * 首页用户重置密码
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
	public Result<?> changPassword(@RequestBody JSONObject json) {
		String username = json.getString("username");
		String oldpassword = json.getString("oldpassword");
		String password = json.getString("password");
		String confirmpassword = json.getString("confirmpassword");
		SysUser user = this.sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
		if (user == null) {
			return Result.error("用户不存在！");
		}
		return sysUserService.resetPassword(username, oldpassword, password, confirmpassword);
	}

	@RequestMapping(value = "/userRoleList", method = RequestMethod.GET)
	public Result<IPage<SysUser>> userRoleList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
											   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Result<IPage<SysUser>> result = new Result<IPage<SysUser>>();
		Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
		String roleId = req.getParameter("roleId");
		String username = req.getParameter("username");
		IPage<SysUser> pageList = sysUserService.getUserByRoleId(page, roleId, username);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	@RequestMapping(value = "/saveSysUserRole", method = RequestMethod.POST)
	public Result<String> saveSysUserRole(@RequestBody SysUserRoleVO sysUserRoleVO) {
		Result<String> result = new Result<String>();
		try {
			sysUserRoleService.removeAllByRoleId(sysUserRoleVO.getRoleId());
			addSysUserRole(sysUserRoleVO);
			result.setMessage("保存成功!");
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setSuccess(false);
			result.setMessage("出错了: " + e.getMessage());
		}
		return result;
	}

	/**
	 * 给指定角色添加用户
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/addSysUserRole", method = RequestMethod.POST)
	public Result<String> addSysUserRole(@RequestBody SysUserRoleVO sysUserRoleVO) {
		Result<String> result = new Result<String>();
		try {
			String sysRoleId = sysUserRoleVO.getRoleId();
			for (String sysUserId : sysUserRoleVO.getUserIdList()) {
				SysUserRole sysUserRole = new SysUserRole(sysUserId, sysRoleId);
				QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
				queryWrapper.eq("role_id", sysRoleId).eq("user_id", sysUserId);
				SysUserRole one = sysUserRoleService.getOne(queryWrapper);
				if (one == null) {
					sysUserRoleService.save(sysUserRole);
				}

			}
			result.setMessage("添加成功!");
			result.setSuccess(true);
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setSuccess(false);
			result.setMessage("出错了: " + e.getMessage());
			return result;
		}
	}

	/**
	 * 删除指定角色的用户关系
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/deleteUserRole", method = RequestMethod.DELETE)
	public Result<SysUserRole> deleteUserRole(@RequestParam(name = "roleId") String roleId,
											  @RequestParam(name = "userId", required = true) String userId
	) {
		Result<SysUserRole> result = new Result<SysUserRole>();
		try {
			QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
			queryWrapper.eq("role_id", roleId).eq("user_id", userId);
			sysUserRoleService.remove(queryWrapper);
			result.success("删除成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("删除失败！");
		}
		return result;
	}

	/**
	 * 批量删除指定角色的用户关系
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/deleteUserRoleBatch", method = RequestMethod.DELETE)
	public Result<SysUserRole> deleteUserRoleBatch(
			@RequestParam(name = "roleId") String roleId,
			@RequestParam(name = "userIds", required = true) String userIds) {
		Result<SysUserRole> result = new Result<SysUserRole>();
		try {
			QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
			queryWrapper.eq("role_id", roleId).in("user_id", Arrays.asList(userIds.split(",")));
			sysUserRoleService.remove(queryWrapper);
			result.success("删除成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("删除失败！");
		}
		return result;
	}

	/**
	 * 部门用户列表
	 */
	@RequestMapping(value = "/departUserList", method = RequestMethod.GET)
	public Result<IPage<SysUser>> departUserList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Result<IPage<SysUser>> result = new Result<IPage<SysUser>>();
		Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
		String depId = req.getParameter("depId");
		String username = req.getParameter("username");
		//根据部门ID查询,当前和下级所有的部门IDS
		List<String> subDepids = new ArrayList<>();
		//部门id为空时，查询我的部门下所有用户
		if (oConvertUtils.isEmpty(depId)) {
			LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			int userIdentity = user.getUserIdentity() != null ? user.getUserIdentity() : CommonConstant.USER_IDENTITY_1;
			if (oConvertUtils.isNotEmpty(userIdentity) && userIdentity == CommonConstant.USER_IDENTITY_2) {
				subDepids = sysDepartService.getMySubDepIdsByDepId(user.getDepartIds());
			}
		} else {
			subDepids = sysDepartService.getSubDepIdsByDepId(depId);
		}
		if (subDepids != null && subDepids.size() > 0) {
			IPage<SysUser> pageList = sysUserService.getUserByDepIds(page, subDepids, username);
			//批量查询用户的所属部门
			//step.1 先拿到全部的 useids
			//step.2 通过 useids，一次性查询用户的所属部门名字
			List<String> userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList());
			if (userIds != null && userIds.size() > 0) {
				Map<String, String> useDepNames = sysUserService.getDepNamesByUserIds(userIds);
				pageList.getRecords().forEach(item -> {
					//批量查询用户的所属部门
					item.setSysOrgCode(useDepNames.get(item.getId()));
				});
			}
			result.setSuccess(true);
			result.setResult(pageList);
		} else {
			result.setSuccess(true);
			result.setResult(null);
		}
		return result;
	}

	/**
	 * 根据 sysOrgCode 查询用户，包括子部门下的用户
	 * 若某个用户包含多个部门，则会显示多条记录，可自行处理成单条记录
	 */
	@GetMapping("/queryByOrgCode")
	public Result<?> queryByDepartId(
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(name = "sysOrgCode") String sysOrgCode,
			SysUser userParams
	) {
		IPage<SysUserSysDepartModel> pageList = sysUserService.queryUserByOrgCode(sysOrgCode, userParams, new Page(pageNo, pageSize));
		return Result.ok(pageList);
	}

	/**
	 * 根据 sysOrgCode 查询用户，包括子部门下的用户
	 * 针对通讯录模块做的接口，将多个部门的用户合并成一条记录，并转成对前端友好的格式
	 */
	@GetMapping("/queryByOrgCodeForAddressList")
	public Result<?> queryByOrgCodeForAddressList(
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(name = "sysOrgCode", required = false) String sysOrgCode,
			SysUser userParams
	) {
		IPage page = new Page(pageNo, pageSize);
		IPage<SysUserSysDepartModel> pageList = sysUserService.queryUserByOrgCode(sysOrgCode, userParams, page);
		List<SysUserSysDepartModel> list = pageList.getRecords();
		// 记录所有出现过的 user, key = userId
		Map<String, JSONObject> hasUser = new HashMap<>(list.size());
		JSONArray resultJson = new JSONArray(list.size());
		for (SysUserSysDepartModel item : list) {
			String userId = item.getId();
			// userId
			JSONObject getModel = hasUser.get(userId);
			// 之前已存在过该用户，直接合并数据
			if (getModel != null) {
				String departName = getModel.get("departName").toString();
				getModel.put("departName", (departName + " | " + item.getDepartName()));
			} else {
				// 将用户对象转换为json格式，并将部门信息合并到 json 中
				JSONObject json = JSON.parseObject(JSON.toJSONString(item));
				json.remove("id");
				json.put("userId", userId);
				json.put("departId", item.getDepartId());
				json.put("departName", item.getDepartName());
//                json.put("avatar", item.getSysUser().getAvatar());
				resultJson.add(json);
				hasUser.put(userId, json);
			}
		}
		IPage<JSONObject> result = new Page<>(pageNo, pageSize, pageList.getTotal());
		result.setRecords(resultJson.toJavaList(JSONObject.class));
		return Result.ok(result);
	}

	/**
	 * 给指定部门添加对应的用户
	 */
	@RequestMapping(value = "/editSysDepartWithUser", method = RequestMethod.POST)
	public Result<String> editSysDepartWithUser(@RequestBody SysDepartUsersVO sysDepartUsersVO) {
		Result<String> result = new Result<String>();
		try {
			String sysDepId = sysDepartUsersVO.getDepId();
			for (String sysUserId : sysDepartUsersVO.getUserIdList()) {
				SysUserDepart sysUserDepart = new SysUserDepart(null, sysUserId, sysDepId);
				QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
				queryWrapper.eq("dep_id", sysDepId).eq("user_id", sysUserId);
				SysUserDepart one = sysUserDepartService.getOne(queryWrapper);
				if (one == null) {
					sysUserDepartService.save(sysUserDepart);
				}
			}
			result.setMessage("添加成功!");
			result.setSuccess(true);
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setSuccess(false);
			result.setMessage("出错了: " + e.getMessage());
			return result;
		}
	}

	/**
	 * 删除指定机构的用户关系
	 */
	@RequestMapping(value = "/deleteUserInDepart", method = RequestMethod.DELETE)
	public Result<SysUserDepart> deleteUserInDepart(@RequestParam(name = "depId") String depId,
													@RequestParam(name = "userId", required = true) String userId
	) {
		Result<SysUserDepart> result = new Result<SysUserDepart>();
		try {
			QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
			queryWrapper.eq("dep_id", depId).eq("user_id", userId);
			boolean b = sysUserDepartService.remove(queryWrapper);
			if (b) {
				List<SysDepartRole> sysDepartRoleList = departRoleService.list(new QueryWrapper<SysDepartRole>().eq("depart_id", depId));
				List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
				if (roleIds != null && roleIds.size() > 0) {
					QueryWrapper<SysDepartRoleUser> query = new QueryWrapper<>();
					query.eq("user_id", userId).in("drole_id", roleIds);
					departRoleUserService.remove(query);
				}
				result.success("删除成功!");
			} else {
				result.error500("当前选中部门与用户无关联关系!");
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("删除失败！");
		}
		return result;
	}

	/**
	 * 批量删除指定机构的用户关系
	 */
	@RequestMapping(value = "/deleteUserInDepartBatch", method = RequestMethod.DELETE)
	public Result<SysUserDepart> deleteUserInDepartBatch(
			@RequestParam(name = "depId") String depId,
			@RequestParam(name = "userIds", required = true) String userIds) {
		Result<SysUserDepart> result = new Result<SysUserDepart>();
		try {
			QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<SysUserDepart>();
			queryWrapper.eq("dep_id", depId).in("user_id", Arrays.asList(userIds.split(",")));
			boolean b = sysUserDepartService.remove(queryWrapper);
			if (b) {
				departRoleUserService.removeDeptRoleUser(Arrays.asList(userIds.split(",")), depId);
			}
			result.success("删除成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("删除失败！");
		}
		return result;
	}

	/**
	 * 查询当前用户的所有部门/当前部门编码
	 *
	 * @return
	 */
	@RequestMapping(value = "/getCurrentUserDeparts", method = RequestMethod.GET)
	public Result<Map<String, Object>> getCurrentUserDeparts() {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			List<SysDepart> list = this.sysDepartService.queryUserDeparts(sysUser.getId());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", list);
			map.put("sysOrgCode", sysUser.getSysOrgCode());
			result.setSuccess(true);
			result.setResult(map);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("查询失败！");
		}
		return result;
	}

	/**
	 * 用户注册接口
	 *
	 * @param jsonObject
	 * @param user
	 * @return
	 */
	@PostMapping("/register")
	public Result<JSONObject> userRegister(@RequestBody JSONObject jsonObject, SysUser user) {
		Result<JSONObject> result = new Result<JSONObject>();
		String phone = jsonObject.getString("phone");
		String smscode = jsonObject.getString("smscode");
		Object code = redisUtil.get(phone);
		String username = jsonObject.getString("username");
		//未设置用户名，则用手机号作为用户名
		if (oConvertUtils.isEmpty(username)) {
			username = phone;
		}
		//未设置密码，则随机生成一个密码
		String password = jsonObject.getString("password");
		if (oConvertUtils.isEmpty(password)) {
			password = RandomUtil.randomString(8);
		}
		String email = jsonObject.getString("email");
		SysUser sysUser1 = sysUserService.getUserByName(username);
		if (sysUser1 != null) {
			result.setMessage("用户名已注册");
			result.setSuccess(false);
			return result;
		}
		SysUser sysUser2 = sysUserService.getUserByPhone(phone);
		if (sysUser2 != null) {
			result.setMessage("该手机号已注册");
			result.setSuccess(false);
			return result;
		}
		if (oConvertUtils.isNotEmpty(email)) {
			SysUser sysUser3 = sysUserService.getUserByEmail(email);
			if (sysUser3 != null) {
				result.setMessage("邮箱已被注册");
				result.setSuccess(false);
				return result;
			}
		}
		if (!smscode.equals(code)) {
			result.setMessage("手机验证码错误");
			result.setSuccess(false);
			return result;
		}
		try {
			user.setCreateTime(new Date());// 设置创建时间
			String salt = oConvertUtils.randomGen(8);
			String passwordEncode = PasswordUtil.encrypt(username, password, salt);
			user.setSalt(salt);
			user.setUsername(username);
			user.setRealname(username);
			user.setPassword(passwordEncode);
			user.setEmail(email);
			user.setPhone(phone);
			user.setStatus(CommonConstant.USER_UNFREEZE);
			user.setDelFlag(CommonConstant.DEL_FLAG_0);
			user.setActivitiSync(CommonConstant.ACT_SYNC_0);
			sysUserService.addUserWithRole(user, "ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
			result.success("注册成功");
		} catch (Exception e) {
			result.error500("注册失败");
		}
		return result;
	}

	/**
	 * 根据用户名或手机号查询用户信息
	 *
	 * @param
	 * @return
	 */
	@GetMapping("/querySysUser")
	public Result<Map<String, Object>> querySysUser(SysUser sysUser) {
		String phone = sysUser.getPhone();
		String username = sysUser.getUsername();
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (oConvertUtils.isNotEmpty(phone)) {
			SysUser user = sysUserService.getUserByPhone(phone);
			if (user != null) {
				map.put("username", user.getUsername());
				map.put("phone", user.getPhone());
				result.setSuccess(true);
				result.setResult(map);
				return result;
			}
		}
		if (oConvertUtils.isNotEmpty(username)) {
			SysUser user = sysUserService.getUserByName(username);
			if (user != null) {
				map.put("username", user.getUsername());
				map.put("phone", user.getPhone());
				result.setSuccess(true);
				result.setResult(map);
				return result;
			}
		}
		result.setSuccess(false);
		result.setMessage("验证失败");
		return result;
	}

	/**
	 * 用户手机号验证
	 */
	@PostMapping("/phoneVerification")
	public Result<String> phoneVerification(@RequestBody JSONObject jsonObject) {
		Result<String> result = new Result<String>();
		String phone = jsonObject.getString("phone");
		String smscode = jsonObject.getString("smscode");
		Object code = redisUtil.get(phone);
		if (!smscode.equals(code)) {
			result.setMessage("手机验证码错误");
			result.setSuccess(false);
			return result;
		}
		redisUtil.set(phone, smscode);
		result.setResult(smscode);
		result.setSuccess(true);
		return result;
	}

	/**
	 * 用户更改密码
	 */
	@GetMapping("/passwordChange")
	public Result<SysUser> passwordChange(@RequestParam(name = "username") String username,
										  @RequestParam(name = "password") String password,
										  @RequestParam(name = "smscode") String smscode,
										  @RequestParam(name = "phone") String phone) {
		Result<SysUser> result = new Result<SysUser>();
		if (oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(password) || oConvertUtils.isEmpty(smscode) || oConvertUtils.isEmpty(phone)) {
			result.setMessage("重置密码失败！");
			result.setSuccess(false);
			return result;
		}
		SysUser sysUser = new SysUser();
		Object object = redisUtil.get(phone);
		if (null == object) {
			result.setMessage("短信验证码失效！");
			result.setSuccess(false);
			return result;
		}
		if (!smscode.equals(object)) {
			result.setMessage("短信验证码不匹配！");
			result.setSuccess(false);
			return result;
		}
		sysUser = this.sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username).eq(SysUser::getPhone, phone));
		if (sysUser == null) {
			result.setMessage("未找到用户！");
			result.setSuccess(false);
			return result;
		} else {
			String salt = oConvertUtils.randomGen(8);
			sysUser.setSalt(salt);
			String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), password, salt);
			sysUser.setPassword(passwordEncode);
			this.sysUserService.updateById(sysUser);
			result.setSuccess(true);
			result.setMessage("密码重置完成！");
			return result;
		}
	}

	/**
	 * 根据TOKEN获取用户的部分信息（返回的数据是可供表单设计器使用的数据）
	 *
	 * @return
	 */
	@GetMapping("/getUserSectionInfoByToken")
	public Result<?> getUserSectionInfoByToken(HttpServletRequest request, @RequestParam(name = "token", required = false) String token) {
		try {
			String username = null;
			// 如果没有传递token，就从header中获取token并获取用户信息
			if (oConvertUtils.isEmpty(token)) {
				username = JwtUtil.getUserNameByToken(request);
			} else {
				username = JwtUtil.getUsername(token);
			}
			log.info(" ------ 通过令牌获取部分用户信息，当前用户： " + username);
			// 根据用户名查询用户信息
			SysUser sysUser = sysUserService.getUserByName(username);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sysUserId", sysUser.getId());
			map.put("sysUserCode", sysUser.getUsername()); // 当前登录用户登录账号
			map.put("sysUserName", sysUser.getRealname()); // 当前登录用户真实名称
			map.put("sysOrgCode", sysUser.getSysOrgCode()); // 当前登录用户部门编号
			log.info(" ------ 通过令牌获取部分用户信息，已获取的用户信息： " + map);
			return Result.ok(map);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return Result.error(500, "查询失败:" + e.getMessage());
		}
	}

	/**
	 * 【APP端接口】获取用户列表  根据用户名和真实名 模糊匹配
	 *
	 * @param keyword
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/appUserList")
	public Result<?> appUserList(@RequestParam(name = "keyword", required = false) String keyword,
								 @RequestParam(name = "username", required = false) String username,
								 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		try {
			LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<SysUser>();
			query.eq(SysUser::getActivitiSync, "1");
			query.eq(SysUser::getDelFlag, "0");
			if (oConvertUtils.isNotEmpty(username)) {
				query.eq(SysUser::getUsername, username);
			} else {
				query.and(i -> i.like(SysUser::getUsername, keyword).or().like(SysUser::getRealname, keyword));
			}
			Page<SysUser> page = new Page<>(pageNo, pageSize);
			IPage<SysUser> res = this.sysUserService.page(page, query);
			return Result.ok(res);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return Result.error(500, "查询失败:" + e.getMessage());
		}

	}

	/**
	 * 获取被逻辑删除的用户列表，无分页
	 *
	 * @return logicDeletedUserList
	 */
	@GetMapping("/recycleBin")
	public Result getRecycleBin() {
		List<SysUser> logicDeletedUserList = sysUserService.queryLogicDeleted();
		if (logicDeletedUserList.size() > 0) {
			// 批量查询用户的所属部门
			// step.1 先拿到全部的 userIds
			List<String> userIds = logicDeletedUserList.stream().map(SysUser::getId).collect(Collectors.toList());
			// step.2 通过 userIds，一次性查询用户的所属部门名字
			Map<String, String> useDepNames = sysUserService.getDepNamesByUserIds(userIds);
			logicDeletedUserList.forEach(item -> item.setSysOrgCode(useDepNames.get(item.getId())));
		}
		return Result.ok(logicDeletedUserList);
	}

	/**
	 * 还原被逻辑删除的用户
	 *
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping(value = "/putRecycleBin", method = RequestMethod.PUT)
	public Result putRecycleBin(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		String userIds = jsonObject.getString("userIds");
		if (StringUtils.isNotBlank(userIds)) {
			SysUser updateUser = new SysUser();
			updateUser.setUpdateBy(JwtUtil.getUserNameByToken(request));
			updateUser.setUpdateTime(new Date());
			sysUserService.revertLogicDeleted(Arrays.asList(userIds.split(",")), updateUser);
		}
		return Result.ok("还原成功");
	}

	/**
	 * 彻底删除用户
	 *
	 * @param userIds 被删除的用户ID，多个id用半角逗号分割
	 * @return
	 */
	@RequestMapping(value = "/deleteRecycleBin", method = RequestMethod.DELETE)
	public Result deleteRecycleBin(@RequestParam("userIds") String userIds) {
		if (StringUtils.isNotBlank(userIds)) {
			sysUserService.removeLogicDeleted(Arrays.asList(userIds.split(",")));
		}
		return Result.ok("删除成功");
	}

	/**
	 * 移动端修改用户信息
	 *
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping(value = "/appEdit", method = RequestMethod.PUT)
	public Result<SysUser> appEdit(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			SysUser sysUser = sysUserService.getById(jsonObject.getString("id"));
			sysBaseAPI.addLog("移动端编辑用户，id： " + jsonObject.getString("id"), CommonConstant.LOG_TYPE_2, 2);
			if (sysUser == null) {
				result.error500("未找到对应用户!");
			} else {
				SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
				user.setUpdateTime(new Date());
				user.setPassword(sysUser.getPassword());
				sysUserService.updateById(user);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败!");
		}
		return result;
	}

	/**
	 * 小程序修改用户信息
	 *
	 * @param jsonObject
	 * @return
	 */
	@ApiOperation("小程序编辑用户")
	@RequestMapping(value = "/miniEdit", method = RequestMethod.PUT)
	public Result<SysUser> miniEdit(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			SysUser sysUser = sysUserService.getById(loginUser.getId());
			sysBaseAPI.addLog("小程序编辑用户，id： " + jsonObject.getString("id"), CommonConstant.LOG_TYPE_2, 2);
			if (sysUser == null) {
				result.error500("未找到对应用户!");
			} else {
				String phone = jsonObject.getString("phone");
				String avatarUrl = jsonObject.getString("avatarUrl");
				String nickName = jsonObject.getString("nickName");
				sysUser.setAvatar(avatarUrl);
				sysUser.setPhone(phone);
				sysUser.setRealname(nickName);
				sysUserService.updateById(sysUser);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败!");
		}
		return result;
	}

	/**
	 * 修改船舶类型
	 */
	@ApiOperation("修改船舶类型")
	@RequestMapping(value = "/changeShipType", method = RequestMethod.PUT)
	public Result<?> changeShipType(@RequestBody String shipType) {
		sysUserService.changeShipType(shipType);
		return Result.ok();
	}

	/**
	 * 获取公司编码
	 */
	@ApiOperation("获取公司编码")
	@RequestMapping(value = "/getCompanyCode", method = RequestMethod.GET)
	public Result<?> getCompanyCode() {
		Result result = Result.ok();
		result.setResult(sysUserService.getCompanyCode());
		return result;
	}

	/**
	 * 判断账号是否重复
	 */
	@ApiOperation("判断账号是否存在")
	@RequestMapping(value = "/checkUserName", method = RequestMethod.GET)
	public Result checkUserName(@RequestParam("username") String username, @RequestParam("id") String id) {
		Boolean b = sysUserService.checkUserName(username, id);
		return Result.ok(b);
	}

	/**
	 * 判断身份证是否重复
	 */
	@ApiOperation("判断身份证是否重复")
	@RequestMapping(value = "/checkIdCard", method = RequestMethod.GET)
	public Result checkIdCard(@RequestParam("idCard") String idCard, @RequestParam("id") String id) {
		Boolean b = sysUserService.checkIdCard(idCard, id);
		return Result.ok(b);
	}

	@ApiOperation("查询所有正常状态的账号")
	@RequestMapping("normalUserName")
	public List<Map<String, String>> normalUserName() {
		return sysUserService.normalUserName();
	}

	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	public Result<List<SysDepartTreeModel>> queryTreeList() {
		Result<List<SysDepartTreeModel>> result = new Result<>();
		try {
			List<SysDepartTreeModel> list = sysUserService.queryTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			Log.error(e.getMessage());
		}
		return result;
	}

	@RequestMapping("/updateShipType")
	public Result<?> updateShipType(@RequestParam("shipType") String shipType, @RequestParam("shipTypeName") String shipTypeName) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUser user = sysUserService.getById(sysUser.getId());
		user.setShipType(shipType);
		user.setShipTypeName(shipTypeName);
		sysUserService.updateById(user);
		redisUtil.del(CacheConstant.SYS_USERS_CACHE + "::" + sysUser.getUsername());
		return Result.ok();
	}

	@RequestMapping("/selectDeptUser")
	public Result<?> selectDeptUser(@RequestParam("deptCode") List<String> deptCode){
		List<SysUser> userList = sysUserService.selectDeptUser(deptCode);
		return Result.ok(userList);
	}


}
